
LyLock_SoulShard = { }
table.insert(LyLock_Plugins, LyLock_SoulShard)
LyLock_SoulShard.Frame = "LyLockSoulShardFrame"
LyLock_SoulShard.Button = "LyLockSoulShardButton"

LyLock_SoulShard.Events = {
    "BAG_UPDATE"
}






------------------------------------------------------
-- Soul Shard Settings -------------------------------
------------------------------------------------------	


LyLock_SoulShard.OnSettings = function(action)
    if action=="default" then

        LyLock_Settings.SoulShard_Button = ReturnSetting(LyLock_Settings.SoulShard_Button, true)
        LyLock_Settings.SoulShard_UseBag0 = ReturnSetting(LyLock_Settings.SoulShard_UseBag0, false)
        LyLock_Settings.SoulShard_UseBag1 = ReturnSetting(LyLock_Settings.SoulShard_UseBag1, false)
        LyLock_Settings.SoulShard_UseBag2 = ReturnSetting(LyLock_Settings.SoulShard_UseBag2, false)
        LyLock_Settings.SoulShard_UseBag3 = ReturnSetting(LyLock_Settings.SoulShard_UseBag3, false)
        LyLock_Settings.SoulShard_UseBag4 = ReturnSetting(LyLock_Settings.SoulShard_UseBag4, false)  
            
    elseif action=="save" then
        LyLock_Settings.SoulShard_Button = iif(SoulShard_Button:GetChecked(), true, false);
        LyLock_Settings.SoulShard_UseBag0 = iif(SoulShard_UseBag0:GetChecked(), true, false);
        LyLock_Settings.SoulShard_UseBag1 = iif(SoulShard_UseBag1:GetChecked(), true, false);
        LyLock_Settings.SoulShard_UseBag2 = iif(SoulShard_UseBag2:GetChecked(), true, false);
        LyLock_Settings.SoulShard_UseBag3 = iif(SoulShard_UseBag3:GetChecked(), true, false);
        LyLock_Settings.SoulShard_UseBag4 = iif(SoulShard_UseBag4:GetChecked(), true, false);
        
    elseif action=="load" then
    
        SoulShard_Button:SetChecked(LyLock_Settings.SoulShard_Button);
        SoulShard_UseBag0:SetChecked(LyLock_Settings.SoulShard_UseBag0);
        SoulShard_UseBag1:SetChecked(LyLock_Settings.SoulShard_UseBag1);
        SoulShard_UseBag2:SetChecked(LyLock_Settings.SoulShard_UseBag2);
        SoulShard_UseBag3:SetChecked(LyLock_Settings.SoulShard_UseBag3);
        SoulShard_UseBag4:SetChecked(LyLock_Settings.SoulShard_UseBag4);
        
    end
end


--------------------------------------------------------------------------------------
-- SoulShard Functions ---------------------------------------------------------------
--------------------------------------------------------------------------------------
LyLock_SoulShard.ToolCount = ""

LyLock_SoulShard.ButtonSetupAction = function()
    local rank = GetSpellRank(WORD_DRAINSOUL)
    if rank~=nil then
        LyLockSoulShardButton:SetAttribute("type","spell");
        LyLockSoulShardButton:SetAttribute("spell", WORD_DRAINSOUL.."("..rank..")")
    end
end

LyLock_SoulShard.ButtonOnMouseDown = function()
    
end

LyLock_SoulShard.OnLoad = function()
    SoulShardRefresh()
end
LyLock_SoulShard.OnEvent = function()
    SoulShardRefresh()
end

function SoulShardRefresh()
	SortSoulShards();
    LyLock_SoulShard.ToolCount = "Shards: "..tostring(SoulShardCount());
	LyLockSoulShardCountText:SetText(tostring(SoulShardCount()));
end

function FirstFreeSlot(inbag)
	for inslot=1, GetContainerNumSlots(inbag) do
		if GetContainerItemLink(inbag, inslot)==nil then
			return inslot
		end
	end
	return nil
end

function SortSoulShards()
	local useBag = GetUseBag()
    local notBag = GetNotBag()
	if (useBag~=nil) then
		for bag=0, 4 do
			if (GetBagName(bag)~=nil) then
				if not IsSoulShardBag(bag) then
					for slot=1, GetContainerNumSlots(bag) do
                        if IsSoulShard(bag, slot) then
                            local useSlot = FirstFreeSlot(useBag)
                            if useSlot==nil then return end
                            PickupContainerItem(bag, slot);
                            PickupContainerItem(useBag, useSlot);
                        end
					end
                elseif notBag~=nil then
					for slot=1, GetContainerNumSlots(bag) do
                        if not IsSoulShard(bag, slot) then
                            local notSlot = FirstFreeSlot(notBag)
                            if notSlot==nil then return end
                            PickupContainerItem(bag, slot);
                            PickupContainerItem(notBag, notSlot);
                        end
					end
                end
			end
		end
	end
end
function GetUseBag()
	local useBag = UseSoulShardBag(0, LyLock_Settings.SoulShard_UseBag0)
	if (useBag==nil) then useBag = UseSoulShardBag(1, LyLock_Settings.SoulShard_UseBag1) end
	if (useBag==nil) then useBag = UseSoulShardBag(2, LyLock_Settings.SoulShard_UseBag2) end
	if (useBag==nil) then useBag = UseSoulShardBag(3, LyLock_Settings.SoulShard_UseBag3) end
	if (useBag==nil) then useBag = UseSoulShardBag(4, LyLock_Settings.SoulShard_UseBag4) end
    return useBag
end
function GetNotBag()
	local notBag = NotSoulShardBag(0, LyLock_Settings.SoulShard_UseBag0)
	if (notBag==nil) then notBag = NotSoulShardBag(1, LyLock_Settings.SoulShard_UseBag1) end
	if (notBag==nil) then notBag = NotSoulShardBag(2, LyLock_Settings.SoulShard_UseBag2) end
	if (notBag==nil) then notBag = NotSoulShardBag(3, LyLock_Settings.SoulShard_UseBag3) end
	if (notBag==nil) then notBag = NotSoulShardBag(4, LyLock_Settings.SoulShard_UseBag4) end
    return notBag
end
function UseSoulShardBag(inbag, isuse)
	if isuse and (GetBagName(inbag)~=nil) then
		if (FirstFreeSlot(inbag)~=nil) then return inbag end
	end	
	return nil
end
function NotSoulShardBag(inbag, isuse)
	if (not isuse) and (GetBagName(inbag)~=nil) then
		if (FirstFreeSlot(inbag)~=nil) then return inbag end
	end	
	return nil
end
function IsSoulShardBag(inbag)
	if inbag==0 and LyLock_Settings.SoulShard_UseBag0 then return true end
	if inbag==1 and LyLock_Settings.SoulShard_UseBag1 then return true end
	if inbag==2 and LyLock_Settings.SoulShard_UseBag2 then return true end
	if inbag==3 and LyLock_Settings.SoulShard_UseBag3 then return true end
	if inbag==4 and LyLock_Settings.SoulShard_UseBag4 then return true end
	return false
end


function SoulShardCount()
    local ret = 0
    for bag=0, 4 do
        if (GetBagName(bag)~=nil) then
            for slot=1, GetContainerNumSlots(bag) do
                if IsSoulShard(bag, slot) then ret= ret+1 end
            end
        end
    end
    return ret
end

function IsSoulShard(bag, slot)
    local link= GetContainerItemLink(bag, slot);
    if (link~=nil) then
        if string.find(link,WORD_SOULSHARD) then
            local _,_, itemID= string.find(link, "item:(%d+):");
            if (itemID and tonumber(itemID) == 6265) then
                return true
            else
                return false
            end
        else
            return false
        end
    else
        return nil
    end
end


